﻿using ImageManager.MyExcep;
using SkiaSharp;
using System;

namespace ImageManager.Common
{
    public static class ImageTools
    {
        static Random rdm = new Random();
        static int counter = 10000;
        public static Stream Watermark(Stream file, string text)
        {
            var sKPaint = new SKPaint
            {
                IsAntialias = true,
                Color = SKColor.Parse("#66ffffff"),
                TextAlign = SKTextAlign.Left
            };
            file.Seek(0, SeekOrigin.Begin);
            using (var fileStream = new SKManagedStream(file))
            {
                using (var bitmap = SKBitmap.Decode(fileStream))
                {
                    using (var canvas = new SKCanvas(bitmap))
                    {
                        sKPaint.TextSize = bitmap.Width / 2 / text.Length;
                        var x = bitmap.Width / 4;
                        var y = bitmap.Height / 2 + sKPaint.TextSize / 2;
                        canvas.DrawText(text, x, y, sKPaint);
                        canvas.Flush();
                        using (var image = SKImage.FromBitmap(bitmap))
                        return image.Encode(SKEncodedImageFormat.Jpeg, 80).AsStream();
                    }
                }
            }
        }
        public static Stream Compress(Stream file,decimal maxWidth, int quality)
        {
            using (var fileStream = new SKManagedStream(file))
            {
                file.Seek(0, SeekOrigin.Begin);
                using (var bitmap = SKBitmap.Decode(fileStream))
                {
                    var width = (decimal)bitmap.Width;
                    var height = (decimal)bitmap.Height;
                    var newWidth = width;
                    var newHeight = height;
                    if (width > maxWidth)
                    {
                        newWidth = maxWidth;
                        newHeight = height / width * maxWidth;
                    }
                    using (var resized = bitmap.Resize(new SKImageInfo((int)newWidth, (int)newHeight), SKFilterQuality.Medium))
                    {
                        if (resized != null)
                        {
                            using (var image = SKImage.FromBitmap(resized))
                            return image.Encode(SKEncodedImageFormat.Jpeg, quality).AsStream();
                        }
                    }
                }
            }
            throw new MyArgException();
        }
        public static string CreateImageCode()
        {
            counter++;
            if (counter > 100000) counter = 10000;
            string random = rdm.Next(100,999).ToString();
            return random + counter.ToString();
        }
        public static string[] GetPath(string date)
        {
            var dataPath = ConfigReader.ReadSetting("Image", "DataPath");
            string dayPath = dataPath + date;
            string[] result = new string[] {
                RuntimeVariable.ROOT_PATH + dayPath,
                RuntimeVariable.ROOT_PATH + dayPath + "/IMAGE",
                RuntimeVariable.ROOT_PATH + dayPath + "/PREVIEW",
                RuntimeVariable.ROOT_PATH + dayPath + "/THUMBNAIL"
            };
            if (!Directory.Exists(RuntimeVariable.ROOT_PATH + "/DATA"))
            {
                Directory.CreateDirectory(RuntimeVariable.ROOT_PATH + "/DATA");
            }
            for (int i = 0; i < result.Length; i++)
            {
                if (!Directory.Exists(result[i]))
                {
                    Directory.CreateDirectory(result[i]);
                }
            }
            return result;
        }
    }
}
